package com.jiexin.datasource;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Author: Jay_Liu
 * @Description:  DSH 数据库连接池配置类
 * @Date:
 * @Modified By:
 */
@Configuration
@MapperScan(basePackages={"com.jiexin.dao.dsh"}, sqlSessionTemplateRef="dshSqlSessionTemplate")
public class DSHDSConfig
{
    @Bean(name={"dshDataSource"})
    @Primary
    public DataSource testDataSource(DSHConfig dshConfig)
            throws SQLException
    {
//        DruidXADataSource druidXADataSource = new DruidXADataSource();
//        druidXADataSource.setUrl(dshConfig.getUrl());
//        druidXADataSource.setUsername(dshConfig.getUsername());
//        druidXADataSource.setPassword(dshConfig.getPassword());
//        druidXADataSource.setInitialSize(dshConfig.getInitialSize());
//        druidXADataSource.setMinIdle(dshConfig.getMinIdle());
//        druidXADataSource.setMaxActive(dshConfig.getMaxActive());
//        druidXADataSource.setMaxWait(dshConfig.getMaxWait());
//        druidXADataSource.setPoolPreparedStatements(dshConfig.isPoolPreparedStatements());
//        druidXADataSource.setMaxPoolPreparedStatementPerConnectionSize(dshConfig.getMaxPoolPreparedStatementPerConnectionSize());
//        druidXADataSource.setValidationQuery(dshConfig.getValidationQuery());
//        druidXADataSource.setTestWhileIdle(dshConfig.isTestWhileIdle());
//        //druidXADataSource.setRemoveAbandoned(dshConfig.isRemoveAbandoned());
//        //druidXADataSource.setRemoveAbandonedTimeout(dshConfig.getRemoveAbandonedTimeout());
//        druidXADataSource.setConnectionProperties(dshConfig.getConnectionProperties());
//        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
//        atomikosDataSourceBean.setXaDataSource(druidXADataSource);
//        atomikosDataSourceBean.setUniqueResourceName("dsh");
//        atomikosDataSourceBean.setMaxPoolSize(dshConfig.getMaxPoolSize());
//        atomikosDataSourceBean.setBorrowConnectionTimeout(dshConfig.getBorrowConnectionTimeout());

        DruidXADataSource druidXADataSource = new DruidXADataSource();
        druidXADataSource.setUrl(dshConfig.getUrl());
        druidXADataSource.setUsername(dshConfig.getUsername());
        druidXADataSource.setPassword(dshConfig.getPassword());
        druidXADataSource.setInitialSize(dshConfig.getInitialSize());
        druidXADataSource.setMinIdle(dshConfig.getMinIdle());
        druidXADataSource.setMaxActive(dshConfig.getMaxActive());
        druidXADataSource.setMaxWait(dshConfig.getMaxWait());
        druidXADataSource.setPoolPreparedStatements(dshConfig.isPoolPreparedStatements());
        druidXADataSource.setMaxPoolPreparedStatementPerConnectionSize(dshConfig.getMaxPoolPreparedStatementPerConnectionSize());
        druidXADataSource.setValidationQuery(dshConfig.getValidationQuery());
        druidXADataSource.setTestWhileIdle(dshConfig.isTestWhileIdle());
        druidXADataSource.setRemoveAbandoned(dshConfig.isRemoveAbandoned());
        druidXADataSource.setRemoveAbandonedTimeout(dshConfig.getRemoveAbandonedTimeout());
        druidXADataSource.setConnectionProperties(dshConfig.getConnectionProperties());
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(druidXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("dsh");

        atomikosDataSourceBean.setMaxPoolSize(dshConfig.getMaxPoolSize());
        atomikosDataSourceBean.setBorrowConnectionTimeout(dshConfig.getBorrowConnectionTimeout());

        return atomikosDataSourceBean;
    }

    @Bean(name={"dshSqlSessionFactory"})
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("dshDataSource") DataSource dataSource)
            throws Exception
    {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/DSH/*.xml"));
        return bean.getObject();
    }

    @Bean(name={"dshSqlSessionTemplate"})
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("dshSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception
    {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name={"dshDataSourceJdbcTemplate"})
    public JdbcTemplate dshDataSourceJdbcTemplate(@Qualifier("dshDataSource") DataSource dataSource)
    {
        return new JdbcTemplate(dataSource);
    }
}
